const app = getApp();
import HttpRequst from '../../../utils/httpRequst.js'
import API from '../../../utils/config.js'
import WxValidate from '../../../plugin/WxValidate'
import SendSMS from '../../../utils/sendSMS.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    biz_id:"",
    telphone: "",
    yzmVal: "",
    yzmRemainTime: 60,
    isYzmInputing: "",//是否验证码输入期间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initValidate();
    this.setData({
      biz_id: options.biz_id
    });
  },
  //实时获取手机号码 
  getTelphoneVal: function (e) {
    this.setData({
      telphone: e.detail.value
    });
    
  },
  inputYzmCode: function (e) {
    this.setData({
      yzmVal: e.detail.value
    });

  },
  getYzmCode: function () {
    var that = this;
    if (this.data.telphone && /^1[23456789]\d{9}$/.test(this.data.telphone)) {
      //验证通过发送手机号码到服务端
      // sendSMS(params, method, loading, sessionChoose, callBack)
      
      SendSMS({ mobile: this.data.telphone }, "POST", false, 2, function (res) {
        

        if (!res.status) {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
            duration: 2000
          })
        } else {
          //服务端发送短信成功
          that.setData({
            isYzmInputing: true,
            yzmVal: ""
          })
          that._countdownYzm();
        }

      });
    } else {
      wx.showToast({
        title:"请输入正确的手机号码",
        icon: 'none',
        duration: 2000
      })
     
    }
  },
  _countdownYzm: function () {
    var that = this;
    var Timer = setInterval(function () {
      if (that.data.yzmRemainTime <= 1) {
        clearInterval(Timer);
        that.setData({
          yzmRemainTime: 60,
          isYzmInputing: false
        });
      };
      that.setData({
        yzmRemainTime: that.data.yzmRemainTime - 1
      });
    }, 1000)
  },
  formSubmit:function(e){
    const params = e.detail.value;
    var that = this;

    //验证表单
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0];
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 2000
      })
    } else {
      HttpRequst(true, API.resetpaypwd,1, app.getUkey(), params, "POST", false, function (res) {
          if(!res.status){
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }else{
            if (res.status==403) {
              wx.showModal({
                title: '提示',
                content: '您未登录，暂时无法进行密码修改',
                cancelText: '我知道了',
                confirmText: '立即登录',
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/index/login/login',
                    })
                  } else if (res.cancel) {

                  }
                }
              })
            }else{
              wx.showToast({
                title: '修改成功!',
                icon: 'success',
                duration: 2000,
                success: function () {
                  setTimeout(function () {
                    wx.navigateTo({
                      url: '/pages/index/detail/detail?biz_id=' + that.data.biz_id
                    })
                  }, 500)
                }
              })
            }
            
          }
      })
    }
  },
  _initValidate: function () {
    const rules = {
      mobile: {
        required: true,
        tel: true
      },
      code: {
        required: true
      },
      pwd:{
        required:true,
        digits:true,
        minlength: 6
      },
      pwdconfirm:{
        required: true,
        digits: true,
        minlength:6,
        equalTo:"pwd"
      }
    };
    const messages = {
      mobile: {
        required: "请输入您的手机号码",
        tel: "请输入正确的手机号码"
      },
      code: {
        required: "请输入验证码"
      },
      pwd: {
        required: "请输入密码",
        digits:"只能输入数字密码",
        minlength: "密码需要6位数字"
      },
      pwdconfirm: {
        required: "请再次输入密码",
        digits: "只能输入数字密码",
        minlength: "密码需要6位数字",
        equalTo: "两次输入密码不一致"

      }
    };
    this.WxValidate = new WxValidate(rules, messages);
  }
})